<?php

include_once dirname(dirname(__FILE__)) . '/include/common.php';
$core = new core();
$action = isset($_POST["action"]) ? $_POST["action"] : '';
$source_customer_order_id = isset($_POST["source_customer_order_id"]) ? intval($_POST["source_customer_order_id"]) : 0;
$destination_customer_order_id = isset($_POST["destination_customer_order_id"]) ? intval($_POST["destination_customer_order_id"]) : 0;
$source_customer_order = $core->getZenCustomerOrderById($source_customer_order_id);
$destination_customer_order = $core->getZenCustomerOrderById($destination_customer_order_id);
if ($action != 'add' && $action != 'remove') {
    $action = 'add';
}
if (!$source_customer_order || !$destination_customer_order) {
    echo json_encode(array('type' => 1, 'msg' => "Đơn hàng không tồn tại!"));
    die;
} else {
    $order_data = $core->getOrderById($source_customer_order->zen_order_id);
    //get source order name
    $source_order_name = $order_data->zen_order_name;
    $order_data = $core->getOrderById($destination_customer_order->zen_order_id);
    //get destination order name
    $destination_order_name = $order_data->zen_order_name;

    if ($action == 'add') {

        if ($source_customer_order->total_current_money > 0) {
            //cap nhat lai tien cho dot hang trước: tien cọc, tổng cọc, tiền hiện tại
            $payment = $source_customer_order->total_current_money;
            $description = "Chuyển tiền thừa tới đợt hàng " . $destination_order_name;
            $payment_date = date('Y-m-d', time());
            $refund_money = core::roundingMoney($payment * (-1));

            $core->db->query("INSERT INTO zen_payment (customer_order_id,payment,type_payment,description,date_payment,other_customer_order_id)
                        VALUES($source_customer_order_id,$refund_money,5,'$description','$payment_date',$destination_customer_order_id)");

            //update total_payment,total_current_money
            $total_payment = $source_customer_order->total_payment - $payment;
            $total_payment = core::roundingMoney($total_payment);
            $total_current_money = 0;
            $core->db->query("UPDATE zen_customer_order SET total_payment=$total_payment,
                        total_current_money= $total_current_money
                            WHERE customer_order_id=$source_customer_order_id");

            //cap nhat tien cho dot hang hien tai
            $description = "Nhận tiền thừa từ đợt hàng " . $source_order_name;
            $payment = core::roundingMoney($payment);
            $core->db->query("INSERT INTO zen_payment (customer_order_id,payment,type_payment,description,date_payment,other_customer_order_id)
                        VALUES($destination_customer_order_id,$payment,5,'$description','$payment_date',$source_customer_order_id)");
            //update total_payment,total_current_money
            $total_payment = $destination_customer_order->total_payment + $payment;
            $total_payment = core::roundingMoney($total_payment);
            $total_current_money = $destination_customer_order->total_current_money + $payment;
            $total_current_money = core::roundingMoney($total_current_money);

            $core->db->query("UPDATE zen_customer_order SET total_payment=$total_payment,
                        total_current_money= $total_current_money
                            WHERE customer_order_id=$destination_customer_order_id");

        } elseif ($source_customer_order->total_current_money < 0) {
            //cap nhat lai tien cho dot hang trước: tien cọc, tổng cọc, tiền hiện tại
            $payment = $source_customer_order->total_current_money * (-1);
            $description = "Nhận tiền còn thiếu từ đợt hàng " . $destination_order_name;
            $payment_date = date('Y-m-d', time());
            $payment = core::roundingMoney($payment);

            $core->db->query("INSERT INTO zen_payment (customer_order_id,payment,type_payment,description,date_payment,other_customer_order_id)
                        VALUES($source_customer_order_id,$payment,5,'$description','$payment_date',$destination_customer_order_id)");
            //update total_payment,total_current_money
            $total_payment = $source_customer_order->total_payment + $payment;
            $total_payment = core::roundingMoney($total_payment);
            $total_current_money = 0;
            $core->db->query("UPDATE zen_customer_order SET total_payment=$total_payment,
                        total_current_money= $total_current_money
                            WHERE customer_order_id=$source_customer_order_id");

            //cap nhat tien cho dot hang hien tai
            $description = "Trả tiền còn thiếu của đợt hàng " . $source_order_name;
            $money_debt = $payment * (-1);
            $money_debt = core::roundingMoney($money_debt);

            $core->db->query("INSERT INTO zen_payment (customer_order_id,payment,type_payment,description,date_payment,other_customer_order_id)
                        VALUES($destination_customer_order_id,$money_debt,5,'$description','$payment_date',$source_customer_order_id)");
            //update total_payment,total_current_money
            $total_payment = $destination_customer_order->total_payment - $payment;
            $total_payment = core::roundingMoney($total_payment);
            $total_current_money = $destination_customer_order->total_current_money - $payment;
            $total_current_money = core::roundingMoney($total_current_money);

            $core->db->query("UPDATE zen_customer_order SET total_payment=$total_payment,
                        total_current_money= $total_current_money
                            WHERE customer_order_id=$destination_customer_order_id");
        }
        $html = $core->renderPayment($destination_customer_order_id);
        echo json_encode(array('type' => 0, 'msg' => "Thanh toán cho đợt hàng " . $source_order_name . " thành công!", 'html' => $html));
        die;
    } else {
        //hủy chuyển tiền
        $payment_id = isset($_POST["payment_id"]) ? intval($_POST["payment_id"]) : 0;
        $data_payment = $core->getPaymentById($payment_id);
        if ($data_payment) {
            $money = $data_payment->payment;
            //xóa đặt cọc chuyen tien cua 2 don hang
            $core->db->query("DELETE FROM zen_payment  
                                        WHERE (customer_order_id=$destination_customer_order_id AND other_customer_order_id= $source_customer_order_id) OR
                                                (customer_order_id=$source_customer_order_id AND other_customer_order_id= $destination_customer_order_id)");

            if ($money > 0) {//trả lại tiền cho đợt hàng trước
                //update total_payment,total_current_money
                $total_payment = $source_customer_order->total_payment + $money;
                $total_payment = core::roundingMoney($total_payment);
                $total_current_money = core::roundingMoney($money);

                $core->db->query("UPDATE zen_customer_order SET total_payment=$total_payment,
                        total_current_money= $total_current_money
                            WHERE customer_order_id=$source_customer_order_id");

                //cap nhat tien cho dot hang hien tai
                $total_payment = $destination_customer_order->total_payment - $money;
                $total_payment = core::roundingMoney($total_payment);
                $total_current_money = $destination_customer_order->total_current_money - $money;
                $total_current_money = core::roundingMoney($total_current_money);

                $core->db->query("UPDATE zen_customer_order SET total_payment=$total_payment,
                        total_current_money= $total_current_money
                            WHERE customer_order_id=$destination_customer_order_id");

            } elseif ($money < 0) {//lay lai tien tu don hang truoc
                //update total_payment,total_current_money
                $total_payment = $source_customer_order->total_payment - $money*(-1);
                $total_payment = core::roundingMoney($total_payment);
                $total_current_money = core::roundingMoney($money);

                $core->db->query("UPDATE zen_customer_order SET total_payment=$total_payment,
                        total_current_money= $total_current_money
                            WHERE customer_order_id=$source_customer_order_id");

                //cap nhat tien cho dot hang hien tai
                 //update total_payment,total_current_money
                $total_payment = $destination_customer_order->total_payment + $money*(-1);
                $total_payment = core::roundingMoney($total_payment);
                $total_current_money = $destination_customer_order->total_current_money + $money*(-1);
                $total_current_money = core::roundingMoney($total_current_money);

                $core->db->query("UPDATE zen_customer_order SET total_payment=$total_payment,
                        total_current_money= $total_current_money
                            WHERE customer_order_id=$destination_customer_order_id");

            }
            $html = $core->renderPayment($destination_customer_order_id);
            echo json_encode(array('type' => 0, 'msg' => "Hủy thanh toán cho đợt hàng " . $source_order_name . " thành công!", 'html' => $html));
            die;
        } else {
            echo json_encode(array('type' => 1, 'msg' => "Có lỗi xảy ra!"));
            die;
        }
    }
}
?>
